<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>校园二手市场 - 浏览历史</title>
    <style>
        :root {
            --primary-color: #4CAF50;
            --secondary-color: #66BB6A;
            --light-gray: #F5F5F5;
            --text-color: #212121;
            --sub-text: #757575;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }

        body {
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--light-gray);
            min-height: 100vh;
            padding-bottom: 60px;
            position: relative;
        }

        .header {
            background-color: var(--primary-color);
            color: white;
            padding: 1rem;
            text-align: center;
        }

        .content {
            flex: 1;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 1rem;
        }

        .browse-history {
            background-color: #fff;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .history-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 1rem;
            margin-top: 1rem;
        }

        .history-item {
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            padding: 1rem;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .history-item:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .history-item img {
            width: 100%;
            height: 150px;
            object-fit: cover;
            border-radius: 4px;
            margin-bottom: 0.5rem;
        }

        .history-item h3 {
            margin-bottom: 0.5rem;
            font-size: 1rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .history-item p {
            color: var(--sub-text);
            margin-bottom: 0.5rem;
            font-size: 0.8rem;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .history-item .price {
            font-weight: bold;
            color: #e74c3c;
            font-size: 1.1rem;
        }

        .history-item .date {
            font-size: 0.7rem;
            color: #95a5a6;
            margin-top: 0.3rem;
        }

        .empty-message {
            text-align: center;
            padding: 2rem;
            color: var(--sub-text);
        }

        .nav-bar {
            display: flex;
            justify-content: space-around;
            background-color: #fff;
            padding: 0.5rem;
            position: fixed;
            bottom: 0;
            width: 100%;
            margin: 0 auto;
            left: 50%;
            transform: translateX(-50%);
            box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
            border-radius: 12px 12px 0 0;
            z-index: 102;
        }

        .nav-bar a {
            color: var(--sub-text);
            text-decoration: none;
            padding: 0.5rem 1rem;
            display: flex;
            flex-direction: column;
            align-items: center;
            font-size: 0.8rem;
            transition: color 0.3s;
            position: relative;
        }

        .nav-bar a.active {
            color: var(--primary-color);
        }

        .nav-bar a.active::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60%;
            height: 3px;
            background-color: var(--primary-color);
            border-radius: 3px;
        }

        .nav-icon {
            width: 24px;
            height: 24px;
            position: relative;
            margin-bottom: 0.2rem;
        }

        .home-icon::before {
            content: "🏠";
        }

        .products-icon::before {
            content: "📦";
        }

        .publish-icon::before {
            content: "✏️";
        }

        .news-icon::before {
            content: "💬";
        }

        .account-icon::before {
            content: "👤";
        }
    </style>
</head>

<body>
    <header class="header">
        <h1>校园二手市场</h1>
    </header>

    <div class="content">
        <main class="container">
            <section class="browse-history">
                <h2>浏览历史</h2>
                <div id="history-list" class="history-list">
                    <!-- 浏览历史商品将通过JavaScript动态加载 -->
                </div>
            </section>
        </main>
    </div>

    <nav class="nav-bar">
        <a href="index.html" class="active">
            <div class="nav-icon home-icon"></div>
            首页
        </a>
        <a href="products.html">
            <div class="nav-icon products-icon"></div>
            商品
        </a>
        <a href="publish.html">
            <div class="nav-icon publish-icon"></div>
            发布
        </a>
        <a href="news.html">
            <div class="nav-icon news-icon"></div>
            消息
        </a>
        <a href="account.html">
            <div class="nav-icon account-icon"></div>
            我的
        </a>
    </nav>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const currentUser = JSON.parse(localStorage.getItem('currentUser'));
            if (!currentUser) {
                window.location.href = 'login.html';
            }

            const historyList = document.getElementById('history-list');
            
            function formatDate(dateString) {
                const date = new Date(dateString);
                return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
            }

            function renderBrowseHistory() {
                const browseHistory = JSON.parse(localStorage.getItem('browseHistory')) || [];
                
                if (browseHistory.length === 0) {
                    historyList.innerHTML = '<p class="empty-message">暂无浏览历史</p>';
                    return;
                }
                
                historyList.innerHTML = '';
                
                browseHistory.forEach(product => {
                    const historyItem = document.createElement('div');
                    historyItem.className = 'history-item';
                    
                    const today = new Date();
                    const productDate = new Date(product.date);
                    const diffInHours = Math.floor((today - productDate) / (1000 * 60 * 60));

                    historyItem.innerHTML = `
                        <img src="${product.image}" alt="${product.title}">
                        <h3>${product.title}</h3>
                        <p>${product.description}</p>
                        <div style="display: flex; justify-content: space-between; align-items: center;">
                            <span class="price">¥${product.price}</span>
                            ${diffInHours <= 24 ? '<span class="new-badge">新</span>' : ''}
                        </div>
                        <p class="date">发布于: ${formatDate(product.date)}</p>
                    `;
                    
                    historyItem.addEventListener('click', function () {
                        window.location.href = `product-detail.html?id=${product.id}`;
                    });
                    
                    historyList.appendChild(historyItem);
                });
            }
            
            renderBrowseHistory();
        });
    </script>
</body>

</html>